# 打开本地摄像头，使用 ffmpeg 进行推流
# 使用前需要安装 ffmpeg，并且服务端没有问题

import queue
import threading
import cv2
import subprocess as sp
 
# 自行设置,url为推送的服务器地址
rtmpUrl = "rtmp://192.168.1.107:1935/live/test"
cap = cv2.VideoCapture(0)
 
fps = int(cap.get(cv2.CAP_PROP_FPS))
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
 
# ffmpeg command
command = ['ffmpeg',
                '-y',
                '-f', 'rawvideo',
                '-vcodec', 'rawvideo',
                '-pix_fmt', 'bgr24',
                '-s', "{}x{}".format(width, height),
                '-r', str(fps),
                '-i', '-',
                '-c:v', 'libx264',
                '-pix_fmt', 'yuv420p',
                '-preset', 'ultrafast',
                '-f', 'flv',
                rtmpUrl]
 
# 设置管道
p = sp.Popen(command, stdin=sp.PIPE)
while True:
    ret, frame = cap.read()
    p.stdin.write(frame.tostring())

